library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.5     ✓ purrr   0.3.4
## ✓ tibble  3.1.6     ✓ dplyr   1.0.8
## ✓ tidyr   1.2.0     ✓ stringr 1.4.0
## ✓ readr   2.1.2     ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
library(readxl)
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(RColorBrewer)
knitr::opts_chunk$set(echo = TRUE)

Read the data, run a summary on the data and create two new columns Range_Miles and PriceDollars

ev_cars <- read_csv("ElectricCarData_Clean.csv")
## Rows: 103 Columns: 14
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (8): Brand, Model, FastCharge_KmH, RapidCharge, PowerTrain, PlugType, Bo...
## dbl (6): AccelSec, TopSpeed_KmH, Range_Km, Efficiency_WhKm, Seats, PriceEuro
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
summary(ev_cars)
##     Brand              Model              AccelSec       TopSpeed_KmH  
##  Length:103         Length:103         Min.   : 2.100   Min.   :123.0  
##  Class :character   Class :character   1st Qu.: 5.100   1st Qu.:150.0  
##  Mode  :character   Mode  :character   Median : 7.300   Median :160.0  
##                                        Mean   : 7.396   Mean   :179.2  
##                                        3rd Qu.: 9.000   3rd Qu.:200.0  
##                                        Max.   :22.400   Max.   :410.0  
##     Range_Km     Efficiency_WhKm FastCharge_KmH     RapidCharge       
##  Min.   : 95.0   Min.   :104.0   Length:103         Length:103        
##  1st Qu.:250.0   1st Qu.:168.0   Class :character   Class :character  
##  Median :340.0   Median :180.0   Mode  :character   Mode  :character  
##  Mean   :338.8   Mean   :189.2                                        
##  3rd Qu.:400.0   3rd Qu.:203.0                                        
##  Max.   :970.0   Max.   :273.0                                        
##   PowerTrain          PlugType          BodyStyle           Segment         
##  Length:103         Length:103         Length:103         Length:103        
##  Class :character   Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
##                                                                             
##                                                                             
##                                                                             
##      Seats         PriceEuro     
##  Min.   :2.000   Min.   : 20129  
##  1st Qu.:5.000   1st Qu.: 34430  
##  Median :5.000   Median : 45000  
##  Mean   :4.883   Mean   : 55812  
##  3rd Qu.:5.000   3rd Qu.: 65000  
##  Max.   :7.000   Max.   :215000
ev_cars <- ev_cars%>%
  mutate(Range_Miles = round(Range_Km * 0.621371,0)) %>%
  mutate(PriceDollars = round(PriceEuro * 1.09))

Get the Top 10 EVs by range in miles and create a Bar graph showing the Brand, Model and Range in Miles

ev_cars_top10 <- ev_cars[order(-ev_cars$Range_Miles),]

ev_cars_top10 <- ev_cars_top10[1:10,]

ev_cars_top10 %>%
  ggplot(aes(fill =Model, y = Range_Miles, x = Brand)) + 
  geom_bar(position="dodge", stat="identity") +
  xlab("Electric Vehicles") +
  ylab("Range in Miles") +
  ggtitle("Range in Miles for Top 10 EV Models") +
  theme(plot.title = element_text(hjust = 0.5),axis.text.x = element_text(angle = 45)) +
  scale_fill_brewer(type = "qual", palette = 3)

Calculate the mean efficiency of each Brand and generate a Bar Plot showing Top 20 Brands by Mean Efficiency

ev_cars_mean_effi_brand <- ev_cars %>%
  group_by(Brand) %>%
  summarise(mean_efficiency = round(mean(Efficiency_WhKm),0))

ev_cars_mean_effi_brand <- ev_cars_mean_effi_brand[order(-ev_cars_mean_effi_brand$mean_efficiency),]
ev_cars_mean_effi_brand %>%
  mutate(Brand = fct_rev(fct_reorder(Brand, mean_efficiency))) %>%
  head(n = 20) %>%
  ggplot(aes(Brand, mean_efficiency,fill = Brand)) +
  geom_bar(position="dodge", stat="identity") +
  xlab("Brand") +
  ylab("Mean Efficiency") +
  ggtitle("Top 20 Brands by Mean Efficiency") +
  theme(plot.title = element_text(hjust = 0.5),axis.text.x = element_text(angle = 45), legend.position="none") 

Generate a Boxplots showing the Price by Body Style

ev_cars %>%
  ggplot(aes(BodyStyle, PriceDollars, fill = BodyStyle)) +
  geom_boxplot() +
  xlab("Body Style") +
  ylab("Price in Dollars") +
  ggtitle("Price by Body Style") +
  theme(plot.title = element_text(hjust = 0.5), legend.position="none") 

ggplotly()

As Tesla is one of the most well known EV brands in the market, below is a bubble graph showing all Tesla models by Price, Range and Body Style.

ev_cars %>%
  filter(Brand == "Tesla") %>%
  ggplot(aes(x = Model, y = PriceDollars, size = Range_Miles, color = BodyStyle)) +
  geom_point(alpha = 0.5) +
  scale_size(range = c(1, 15)) +
  xlab("Tesla Models") +
  ylab("Price in Dollars") +
  ggtitle("A Closer Look at the Different Tesla Models") + 
  theme(plot.title = element_text(hjust = 0.5),axis.text.x = element_text(angle = 90))

ggplotly()